from fastapi import FastAPI
from typing import Union #https://blog.csdn.net/u014651560/article/details/118542887

app = FastAPI()

'''
2.1 查询参数
'''
@app.get("/items1/")
async def read_item(skip: int, limit: int):
    return {"skip": skip,"limit": limit}



'''
2.2 查询参数设置默认值
'''
@app.get("/items2/")
async def read_item(skip: int = 0, limit: int = 10):
    return {"skip": skip,"limit": limit}


'''
2.3 可选参数
'''
@app.get("/items3/{item_id}")
async def read_item(item_id: str, q: Union[str, None]=None):
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}


'''
2.4 类型转换
'''
@app.get("/items4/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None, short: bool = False):
    item = {"item_id": item_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)
